<?php
class PaymentChargingWapmasterController extends CpController
{
    public function actionIndex()
    {
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 30;
        
        $search = array();
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "01-" . date("m-Y");        
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : date("d-m-Y");        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["telco"] = isset($_GET["telco"]) ? trim(strip_tags($_GET["telco"])) : "";        
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["content_type"] = isset($_GET["content_type"]) ? trim(strip_tags($_GET["content_type"])) : "";        
        
        /* Danh mục charging */
        $content_type = LoadConfig::$content_type_charging;
        
        $report = array();
        $totalUser = 0;        
        $numberPage = 0;        
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){
            
            /* Lấy danh sách user phát sinh doanh thu trong khoảng thời gian tìm kiếm */
            $users = AChargingWapmaster::GetUser($search, $currentPage, $numberRecordPerPage);
            $totalUser = AChargingWapmaster::GetTotalUser($search);
            $numberPage = ceil($totalUser / $numberRecordPerPage);
            
            if(!empty($users)){
                $search["list_user_id"] = "";
                foreach($users as $row){
                    $search["list_user_id"] .= ", " . $row["userId"];
                }        
                $search["list_user_id"] = ltrim($search["list_user_id"], ", ");
                
                /* Lấy log sms của các thành viên này */
                $report = AChargingWapmaster::getReport($search);                
            }   
        }        
        
        $this->render(
            "index"
            , array(
                "search"=>$search
                , "content_type"=>$content_type
                , "report"=>$report
                , "totalUser"=>$totalUser
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage
            )
        );   
    }
    
    public function actionDetail()
    {
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? $currentPage=1 : $currentPage;
        $numberRecordPerPage = 50;
        
        $search = array();
        $search["userId"] = isset($_GET["userId"]) ? intval($_GET["userId"]) : 0;
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "01-" . date("m-Y");        
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : date("d-m-Y");        
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["telco"] = isset($_GET["telco"]) ? trim(strip_tags($_GET["telco"])) : "";        
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["content_type"] = isset($_GET["content_type"]) ? trim(strip_tags($_GET["content_type"])) : "";        
        
        /* Danh mục charging */
        $content_type = LoadConfig::$content_type_charging;
        $type_content = array();
        foreach($content_type as $key=>$value){
            $type_content[$value] = $key;   
        }
        
        $charging = array();
        $totalCharging = 0;        
        $numberPage = 0;
        
        if(!empty($search["toDate"]) && !empty($search["fromDate"]) && (strtotime($search["toDate"]) <= strtotime($search["fromDate"])) ){                        
            /* Lấy log sms của các thành viên này */
            $charging = AChargingWapmaster::getReportByUser($search, $currentPage, $numberRecordPerPage);
            $totalCharging = AChargingWapmaster::getTotalReportByUser($search);            
            $numberPage = ceil($totalCharging / $numberRecordPerPage);
        }

        $this->render(
            "detail"
            , array(
                "search"=>$search                
                , "charging"=>$charging
                , "totalCharging"=>$totalCharging
                , "numberPage"=>$numberPage
                , "currentPage"=>$currentPage
                , "content_type"=>$content_type
                , "type_content"=>$type_content
            )
        );
    }
}  
?>
